import { Body, Controller, Delete, Get, Post, Query } from "@nestjs/common";
import { ResType } from "st-common-req";
import { genGetRes } from "st-common-server";
import { MenuService } from "./menu.service";
import { Menu } from "./entities/Menu";
import { MenuResultMessage } from "./consts/menu-result-message";

/**
 * 菜单控制器
 */
@Controller("menu")
export class MenuController {

  constructor(
    private readonly menuService: MenuService
  ) {
  }

  @Get()
  async get(@Query() filters: Record<string, any>, @Query("isTree") isTree: boolean = true): Promise<ResType> {
    // 获取菜单
    return genGetRes(
      await this.menuService.find(filters, isTree),
      MenuResultMessage.GET_SUCCESS,
      MenuResultMessage.GET_FAIL
    )
  }

  @Post()
  async post(@Body() menu: Menu) {
    return await this.menuService.save(menu);
  }

  @Delete()
  async delete(@Body("id") ids: number[], @Body("isCascade") isCascade: boolean) {
    return await this.menuService.remove(ids, isCascade);
  }

}
